package com.wlyuan.open.scanner.assembler;

import com.wlyuan.core.domain.MappingIds;
import com.wlyuan.open.dto.OpenTransportSyncDTO;
import com.wlyuan.open.scanner.domain.transport.Transport;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class ScannerTransportAssembler {

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    @Mapper(uses = {MappingInvalid.class, MappingIds.class})
    public interface Assembler {

        /**
         * 转换
         *
         * @param source
         * @return
         */
        @Mappings({
                @Mapping(source = "createTime", target = "createAt"),
                @Mapping(source = "updateTime", target = "updateAt"),
                @Mapping(source = "info.createBy", target = "createBy"),
                @Mapping(source = "info.updateBy", target = "updateBy"),
                @Mapping(source = "info.upTransportId", target = "sourceWaybillId"),
                @Mapping(source = "info.contractNo", target = "contractNo"),
                @Mapping(source = "info.createType", target = "source"),
                @Mapping(source = "info.waybillType", target = "waybillType"),
                @Mapping(source = "info.orderId", target = "orderId"),
                @Mapping(source = "info.orderTenantId", target = "orderTenantId"),
                @Mapping(source = "info.orderCustomerId", target = "orderCustomerId"),
                @Mapping(source = "info.orderCustomerType", target = "orderCustomerType"),
                @Mapping(source = "info.orderCustomerName", target = "orderCustomerName"),
                @Mapping(source = "info.orderCreateBy", target = "orderCreateBy"),
                @Mapping(source = "info.orderNo", target = "orderNo"),
                @Mapping(source = "from.fromTime", target = "fromTime"),
                @Mapping(source = "from", target = "fromAddress"),
                @Mapping(source = "from.locationType", target = "fromAddress.precision"),
                @Mapping(source = "to.toTime", target = "toTime"),
                @Mapping(source = "to", target = "toAddress"),
                @Mapping(source = "to.locationType", target = "toAddress.precision"),
                @Mapping(source = "info.totalWeight", target = "totalWeight"),
                @Mapping(source = "info.totalVolume", target = "totalVolume"),
                @Mapping(source = "info.totalValue", target = "totalValue"),
                @Mapping(source = "info.totalAmount", target = "totalQuantity"),
                @Mapping(source = "info.weightAddition", target = "differenceWeight"),
                @Mapping(source = "info.volumeAddition", target = "differenceVolume"),
                @Mapping(source = "info.valueAddition", target = "differenceValue"),
                @Mapping(source = "info.desc", target = "remarks"),
                @Mapping(source = "info.consigneeId", target = "consigneeId"),
                @Mapping(source = "info.consigneeName", target = "consigneeName"),
                @Mapping(source = "info.dispatchBy", target = "dispatchBy"),
                @Mapping(source = "info.dispatchAt", target = "dispatchAt"),
                @Mapping(source = "info.followWaybill", target = "followWaybill"),
                @Mapping(source = "info.echo", target = "echo"),
                @Mapping(source = "info.customerId", target = "customerId"),
                @Mapping(source = "info.customerType", target = "customerType"),
                @Mapping(source = "info.customerName", target = "customerName"),

                @Mapping(source = "requirement.pick", target = "arguments.pick"),
                @Mapping(source = "requirement.deliver", target = "arguments.deliver"),
                @Mapping(source = "requirement.onLoad", target = "arguments.load"),
                @Mapping(source = "requirement.offLoad", target = "arguments.unload"),
                @Mapping(source = "requirement.shipMode", target = "arguments.distanceType"),
                @Mapping(source = "requirement.chargeMode", target = "arguments.billingMode"),
                @Mapping(source = "requirement.insure", target = "arguments.insure"),
                @Mapping(source = "requirement.transportMode", target = "arguments.transportMode"),
                @Mapping(source = "requirement.priceUnit", target = "arguments.billingUnit"),
                @Mapping(source = "requirement.goodsCategory", target = "arguments.cargoType"),
                @Mapping(source = "requirement.packageMode", target = "arguments.packageMode"),

                @Mapping(source = "cost", target = "quote"),
                @Mapping(source = "cost.refresh", target = "quote.recalculated"),
                @Mapping(source = "cost.quotationStatus", target = "quote.status"),
                @Mapping(source = "cost.minCost", target = "quote.minimumFee"),

                @Mapping(source = "finance", target = "bill"),

                @Mapping(source = "close.reason", target = "close.closeReason"),
                @Mapping(source = "close.userId", target = "close.closeBy"),
                @Mapping(source = "close.createAt", target = "close.closeAt"),

                @Mapping(source = "vehicle.vehicleType", target = "vehicle.model"),
                @Mapping(source = "vehicle.vehicleLength", target = "vehicle.length"),
                @Mapping(source = "vehicle.vehicleWeight", target = "vehicle.weight"),
                @Mapping(source = "vehicle.vehicleVolume", target = "vehicle.volume"),
                @Mapping(source = "vehicle.vehicleCount", target = "vehicle.quantity"),

                @Mapping(source = "carrier.sign.goodsSign", target = "carrier.sign.signs"),
                @Mapping(source = "carrier.receipt.goodsReceipt", target = "carrier.receipt.receipts"),
                @Mapping(source = "customer.sign.goodsSign", target = "customer.sign.signs"),
                @Mapping(source = "customer.receipt.goodsReceipt", target = "customer.receipt.receipts"),
        })
        OpenTransportSyncDTO assemble(Transport source);

        /**
         * 转换
         *
         * @param source
         * @return
         */
        List<OpenTransportSyncDTO> assemble(List<Transport> source);

    }

    public static List<OpenTransportSyncDTO> assemble(List<Transport> creation) {
        return ASSEMBLER.assemble(creation);
    }

    public static OpenTransportSyncDTO assemble(Transport creation) {
        return ASSEMBLER.assemble(creation);
    }

}
